package cc.higou.core.interceptor;

import cc.higou.core.tools.SessionTool;
import cc.higou.service.SessionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 说明：
 * 自定义的拦截器类，用来拦截有些操作，必须在登录的情况下，才能进行
 *
 * @author:dongxing
 * @time:--
 */

public class MyInterceptor implements HandlerInterceptor {

    @Autowired
    private SessionService sessionService;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 判断登录
        String xingsessid = SessionTool.getSession(request, response);
        String usrname = sessionService.getUserFromRedis(xingsessid);
        if (usrname == null) {
            //未登录的情况
            // 回到登录页面 (登录之后回到购物车页面)
            response.sendRedirect("http://localhost:8082/login.aspx?returnUrl=http://localhost:8083/cart");
            return false;
        } else {
            return true;
        }

    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
